package com.hdcircles.autoform.ui.label.adapter;

import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hdcircles.autoform.R;
import com.hdcircles.autoform.model.bean.FormBean;
import com.hdcircles.autoform.model.bean.User;
import com.hdcircles.autoform.util.SharedPreferenceUtil;
import com.hdcircles.autoform.util.SystemUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by hdcircles on 2016/9/27.
 */

public class FormAdapter extends RecyclerView.Adapter<FormAdapter.ViewHolder> {

    private List<FormBean> formBeanList;

    private FillClickListener fillClickListener;

    private ViewClickListener viewClickListener;

    private onSendClickListener onSendClickListener;

    private onShareClickListener onShareClickListener;

    public FormAdapter(List<FormBean> formBeanList) {
        this.formBeanList = formBeanList;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_form, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        FormBean formBean = formBeanList.get(position);
        holder.tvFormName.setText(formBean.getFormName().toUpperCase());
        holder.tvCompleted.setText(null==formBean.getQuantity()?"0":String.valueOf(formBean.getQuantity()));
        holder.tvShared.setText(String.valueOf(formBean.getShared()));
        if (SharedPreferenceUtil.getUserEmail().equals(formBean.getCreator())){
            holder.ivShare.setVisibility(View.VISIBLE);
        }else {
            holder.ivShare.setVisibility(View.GONE);
        }
        if (null!=formBean.getUser_Form()&&formBean.getUser_Form().size()!=0){
            holder.ivMore.setVisibility(View.VISIBLE);
            holder.ivShare.setVisibility(View.GONE);
            holder.llShare.removeAllViews();
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(SystemUtil.dp2px(holder.itemView.getContext(),24), ViewGroup.LayoutParams.MATCH_PARENT);
            params.setMarginStart(SystemUtil.dp2px(holder.itemView.getContext(),4));
            for (int i=0;i<formBean.getUser_Form().size();i++){
                User user=formBean.getUser_Form().get(i).getUser();
                CircleImageView iv=new CircleImageView(holder.itemView.getContext());
                if (null!=user.getPhoto()){
                    Glide.with(holder.itemView.getContext())
                            .load(user.getPhoto())
                            .into(iv);
                }else {
                    iv.setImageDrawable(holder.itemView.getContext().getDrawable(R.drawable.icon));
                }
                holder.llShare.addView(iv,params);
                if (i==2) break;
            }

        }else {
            holder.ivMore.setVisibility(View.GONE);
            holder.llShare.removeAllViews();
        }
        if (0 != formBean.getTime())
            holder.tvTime.setText(SystemUtil.long2Date(formBean.getTime()));
        holder.tvFill.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fillClickListener.onFill(formBeanList.get(holder.getAdapterPosition()), view);
            }
        });
        holder.tvView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewClickListener.onView(formBeanList.get(holder.getAdapterPosition()).getFormId()
                        ,formBeanList.get(holder.getAdapterPosition()).getCreator(), view);
            }
        });
        holder.ivSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onSendClickListener.onSend(formBeanList.get(holder.getAdapterPosition()),view);
            }
        });
        holder.ivShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onShareClickListener.onShare(formBeanList.get(holder.getAdapterPosition()),view);
            }
        });
        final PopupMenu popupMenu=new PopupMenu(holder.itemView.getContext(),holder.ivMore);
        popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu());
        holder.ivMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupMenu.show();
            }
        });
        popupMenu.getMenu().findItem(R.id.action_view_user).setVisible(false);
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.action_share:
                        onShareClickListener.onShare(formBeanList.get(holder.getAdapterPosition()),holder.ivMore);
                        break;
                    case R.id.action_view_user:
                        break;
                }
                return true;
            }
        });
    }

    public void setOnFillClickListener(FillClickListener fillClickListener) {
        this.fillClickListener = fillClickListener;
    }

    public void setOnViewClickListener(ViewClickListener viewClickListener) {
        this.viewClickListener = viewClickListener;
    }

    public void setOnSendClickListener(onSendClickListener onSendClickListener){
        this.onSendClickListener=onSendClickListener;
    }

    public void setOnShareClickListener(FormAdapter.onShareClickListener onShareClickListener) {
        this.onShareClickListener = onShareClickListener;
    }

    public interface FillClickListener {
        void onFill(FormBean form, View view);
    }

    public interface ViewClickListener {
        void onView(String id,String creatorEmail, View view);
    }

    public interface onSendClickListener{
        void onSend(FormBean form,View view);
    }

    public interface onShareClickListener{
        void onShare(FormBean formBean,View view);
    }

    @Override
    public int getItemCount() {
        return formBeanList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_form_name)
        TextView tvFormName;
        @BindView(R.id.tv_completed)
        TextView tvCompleted;
        @BindView(R.id.tv_shared)
        TextView tvShared;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_fill)
        TextView tvFill;
        @BindView(R.id.tv_view)
        TextView tvView;
        @BindView(R.id.iv_send)
        ImageView ivSend;
        @BindView(R.id.iv_share)
        ImageView ivShare;
        @BindView(R.id.ll_share)
        LinearLayout llShare;
        @BindView(R.id.iv_more)
        ImageView ivMore;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
